<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图像处理工具 - 图片尺寸调整器</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        /* 自定义样式 */
        .upload-area {
            border: 2px dashed #cbd5e1;
            transition: all 0.3s ease;
        }
        
        .upload-area.dragover {
            border-color: #3b82f6;
            background-color: #eff6ff;
        }
        
        .color-preview {
            background-image: 
                linear-gradient(45deg, #f1f5f9 25%, transparent 25%), 
                linear-gradient(-45deg, #f1f5f9 25%, transparent 25%), 
                linear-gradient(45deg, transparent 75%, #f1f5f9 75%), 
                linear-gradient(-45deg, transparent 75%, #f1f5f9 75%);
            background-size: 20px 20px;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
        }
        
        canvas {
            max-width: 100%;
            height: auto;
            border: 1px solid #e5e7eb;
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <div class="container mx-auto px-4 py-8 max-w-6xl">
        <!-- 标题 -->
        <div class="text-center mb-8">
            <h1 class="text-3xl font-bold text-gray-800 mb-2">图像处理工具</h1>
            <p class="text-gray-600">调整图片尺寸并添加背景，支持透明背景</p>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- 左侧控制面板 -->
            <div class="bg-white rounded-lg shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6">设置参数</h2>
                
                <!-- 尺寸设置 -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 mb-3">目标尺寸</label>
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="block text-xs text-gray-500 mb-1">宽度 (px)</label>
                            <input type="number" id="width" value="800" min="1" max="4000" 
                                class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-xs text-gray-500 mb-1">高度 (px)</label>
                            <input type="number" id="height" value="600" min="1" max="4000" 
                                class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                    <!-- 预设尺寸快捷按钮 -->
                    <div class="mt-3">
                        <label class="block text-xs text-gray-500 mb-2">常用尺寸</label>
                        <div class="flex flex-wrap gap-2">
                            <button onclick="setSize(1920, 1080)" class="px-3 py-1 text-xs bg-gray-100 hover:bg-gray-200 rounded">1920×1080</button>
                            <button onclick="setSize(1280, 720)" class="px-3 py-1 text-xs bg-gray-100 hover:bg-gray-200 rounded">1280×720</button>
                            <button onclick="setSize(800, 600)" class="px-3 py-1 text-xs bg-gray-100 hover:bg-gray-200 rounded">800×600</button>
                            <button onclick="setSize(500, 500)" class="px-3 py-1 text-xs bg-gray-100 hover:bg-gray-200 rounded">500×500</button>
                        </div>
                    </div>
                </div>

                <!-- 背景颜色设置 -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 mb-3">背景设置</label>
                    <div class="space-y-3">
                        <!-- 透明背景选项 -->
                        <label class="flex items-center">
                            <input type="radio" name="background" value="transparent" id="bg-transparent" checked 
                                class="mr-2 text-blue-600">
                            <span class="text-sm">透明背景</span>
                        </label>
                        
                        <!-- 颜色背景选项 -->
                        <label class="flex items-center">
                            <input type="radio" name="background" value="color" id="bg-color" 
                                class="mr-2 text-blue-600">
                            <span class="text-sm mr-3">颜色背景</span>
                            <div class="flex items-center space-x-2">
                                <div class="relative">
                                    <input type="color" id="bg-color-picker" value="#ffffff" 
                                        class="w-8 h-8 rounded border cursor-pointer">
                                </div>
                                <input type="text" id="bg-color-hex" value="#ffffff" 
                                    class="w-20 px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500">
                            </div>
                        </label>
                    </div>
                    
                    <!-- 颜色预览 -->
                    <div class="mt-3">
                        <div class="w-full h-12 rounded border color-preview" id="color-preview"></div>
                    </div>
                </div>

                <!-- 图片设置 -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 mb-3">图片设置</label>
                    <div class="space-y-3">
                        <!-- 自适应缩放选项 -->
                        <label class="flex items-center">
                            <input type="radio" name="scale-mode" value="fit" id="scale-fit" checked
                                class="mr-2 text-blue-600">
                            <span class="text-sm">自适应缩放</span>
                            <span class="text-xs text-gray-500 ml-2">（图片会缩放以适应目标尺寸）</span>
                        </label>
                        
                        <!-- 保持原尺寸选项 -->
                        <label class="flex items-center">
                            <input type="radio" name="scale-mode" value="original" id="scale-original"
                                class="mr-2 text-blue-600">
                            <span class="text-sm">保持原尺寸</span>
                            <span class="text-xs text-gray-500 ml-2">（图片保持原始大小，可能被裁剪）</span>
                        </label>
                    </div>
                </div>

                <!-- 文件上传 -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 mb-3">上传图片</label>
                    <div class="upload-area rounded-lg p-8 text-center cursor-pointer" id="upload-area">
                        <input type="file" id="file-input" accept="image/*" class="hidden">
                        <div id="upload-content">
                            <svg class="mx-auto h-12 w-12 text-gray-400 mb-4" stroke="currentColor" fill="none" viewBox="0 0 48 48">
                                <path d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                            <p class="text-gray-600 mb-2">点击上传或拖拽图片到此处</p>
                            <p class="text-xs text-gray-500">支持 JPG、PNG、GIF 格式</p>
                        </div>
                    </div>
                </div>

                <!-- 处理按钮 -->
                <div class="space-y-3">
                    <button id="process-btn" onclick="processImage()" 
                        class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-3 px-4 rounded-md transition-colors disabled:bg-gray-400 disabled:cursor-not-allowed" 
                        disabled>
                        生成图片
                    </button>
                    <button id="download-btn" onclick="downloadImage()" 
                        class="w-full bg-green-600 hover:bg-green-700 text-white font-medium py-3 px-4 rounded-md transition-colors disabled:bg-gray-400 disabled:cursor-not-allowed" 
                        disabled>
                        下载图片
                    </button>
                </div>
            </div>

            <!-- 右侧预览区域 -->
            <div class="bg-white rounded-lg shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6">预览</h2>
                
                <!-- 原图预览 -->
                <div class="mb-6">
                    <h3 class="text-sm font-medium text-gray-700 mb-3">原图</h3>
                    <div class="border-2 border-dashed border-gray-300 rounded-lg p-4 min-h-32 flex items-center justify-center">
                        <div id="original-preview" class="text-gray-500 text-center">
                            <p>暂无图片</p>
                        </div>
                    </div>
                </div>

                <!-- 处理结果预览 -->
                <div>
                    <h3 class="text-sm font-medium text-gray-700 mb-3">处理结果</h3>
                    <div class="border-2 border-dashed border-gray-300 rounded-lg p-4 min-h-32 flex items-center justify-center color-preview">
                        <canvas id="result-canvas" style="display: none;"></canvas>
                        <div id="result-placeholder" class="text-gray-500 text-center">
                            <p>等待处理</p>
                        </div>
                    </div>
                </div>

                <!-- 图片信息显示 -->
                <div id="image-info" class="mt-4 p-3 bg-gray-50 rounded-md text-sm text-gray-600" style="display: none;">
                    <div class="grid grid-cols-2 gap-2">
                        <div>原始尺寸: <span id="original-size">-</span></div>
                        <div>目标尺寸: <span id="target-size">-</span></div>
                        <div>文件大小: <span id="file-size">-</span></div>
                        <div>文件格式: <span id="file-format">-</span></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>